//
//  adviceVC.m
//  BeautyPicture
//
//  Created by 沈振邦 on 15/11/25.
//  Copyright © 2015年 szb. All rights reserved.
//

#import "adviceVC.h"
#import "HMTextView.h"
#import "BPhttpTool.h"
#import "AccountTool.h"
#import "MBProgressHUD+MJ.h"
#import "BPLoginViewController.h"

@interface adviceVC ()<UIScrollViewDelegate>

@property (nonatomic, weak) HMTextView *textView;

@end

@implementation adviceVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"意见反馈";
    [self CreateBoWen];
    [self createText];
}


// 波纹线
- (void)CreateBoWen{
    CGFloat imaH = 64.0f;
    UIImageView *imageBoWen = [[UIImageView alloc] initWithFrame:CGRectMake(0, imaH, self.view.frame.size.width, 7)];
    imageBoWen.image = [UIImage imageNamed:@"波纹线"];
    [self.view addSubview:imageBoWen];
}

- (void)createText{
    scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 72, self.view.frame.size.width, self.view.frame.size.height - 72)];
    scrollview.alwaysBounceVertical = YES;
    scrollview.delegate = self;
    [self.view addSubview:scrollview];
    scrollview.backgroundColor = [UIColor whiteColor];
    
    CGFloat labelY = 15.0;
    CGFloat labelX = BPStatusCellInset + 5;
    CGFloat labelW = BPScreenW - labelX * 2.0;
//    UILabel *promptLabel = [[UILabel alloc] initWithFrame:CGRectMake(labelX, labelY, labelW, labelH)];
//    promptLabel.numberOfLines = 0;
//    
//    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:@"天要下雨，爷要吐槽！点击输入可图文并吐，让不爽更直猛！我们比另一半更在意你的感受"];
//    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
//    //设置行距
//    [style setLineSpacing:6.0f];
//    [style setAlignment:NSTextAlignmentLeft];
//    //根据给定长度与style设置attStr式样
//    [attStr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, attStr.length)];
//    //Label获取attStr式样
//    promptLabel.attributedText = attStr;
//    //Label自适应大小
//    [promptLabel sizeToFit];
//    
//    promptLabel.font = [UIFont systemFontOfSize:16.0f];
//    promptLabel.textColor = [UIColor blackColor];
//    [scrollview addSubview:promptLabel];
    
    HMTextView *textView = [[HMTextView alloc] init];
    [textView.layer setBorderWidth:0.7];
    [textView.layer setBorderColor:[BPGlobaBGColor CGColor]];
    textView.alwaysBounceVertical = YES; // 垂直方向上拥有有弹簧效果
    textView.frame = CGRectMake(labelX, labelY , labelW, 240);
    [scrollview addSubview:textView];
    self.textView = textView;
    // 2.设置提醒文字（占位文字）
    textView.placehoder = @"请输入反馈,我们将为您不断改进";
    // 3.设置字体
    textView.font = [UIFont systemFontOfSize:15];
    
    UIButton *sendmessage = [UIButton buttonWithType:UIButtonTypeCustom];
    CGFloat btnY2 = CGRectGetMaxY(textView.frame) + 50;
    CGFloat btnH = 33.0f;
    CGFloat btnW = self.view.frame.size.width - 60;
    sendmessage.size = CGSizeMake(btnW, btnH);
    sendmessage.center = CGPointMake(BPScreenW / 2.0, btnY2);
    [sendmessage.layer setMasksToBounds:YES];
    [sendmessage.layer setCornerRadius:15.0f];
    sendmessage.backgroundColor = BPGlobaThemeColor;
    [sendmessage setTitle:@"发送" forState:UIControlStateNormal];
    [sendmessage setTintColor:[UIColor whiteColor]];
    [scrollview addSubview:sendmessage];
    [sendmessage addTarget:self action:@selector(sendMessage) forControlEvents:UIControlEventTouchUpInside];
    
    CGFloat aaaY = CGRectGetMaxY(sendmessage.frame) + 15.0f;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, aaaY, BPScreenW, 20)];
//    [scrollview addSubview:label];
//    label.text = @"真心人，吐槽有惊喜哟!";
//    label.textAlignment = NSTextAlignmentCenter;
//    label.font = [UIFont systemFontOfSize:16.0f];
    scrollview.contentSize = CGSizeMake(BPScreenW, label.y + label.height + 20);
}

- (void)sendMessage
{
    if ([AccountTool account].uid != nil) {
        
        //有uid就进行上传照片
        [self posetAdvice];
    }else{
        BPLoginViewController *loginAPP = [[BPLoginViewController alloc] init];
        [self.navigationController pushViewController:loginAPP animated:YES];
        loginAPP.block = ^(){
            [self posetAdvice];
        };
    }
    
    
}

- (void)posetAdvice
{
    NSDictionary *params = @{@"uid":[AccountTool account].uid,
                             @"content":self.textView.text
                             };
    [BPhttpTool post:[NSString stringWithFormat:@"%@appapi/message",BPhttp] params:params success:^(id responseObj) {
        NSDictionary *dic = responseObj;
        if ([dic[@"status"] intValue] == 1) {
            [MBProgressHUD showSuccess:@"提交成功"];
        }else{
            [MBProgressHUD showError:@"提交失败"];
        }
        
    } failure:^(NSError *error) {
        [MBProgressHUD showError:@"提交失败"];
    }];
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
